package com.etone.smartAudit.service;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.etone.smartAudit.error.CustomParameterizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Locale;
import java.util.Map;

@Service
public class RestTemplateRequest {
    @Autowired
    private RestTemplate restTemplate;

    /***
     * post  requestbody请求
     * @param param
     * @param url
     */
    public void postRequestBody(JSONObject param, String url, EGCCHandCallback hand) {
        post(param, url, hand, "application/json;charset=UTF-8", null);
    }

    /***
     * post  requestbody请求
     * @param param
     * @param url
     */
    public void postRequestBody(JSONObject param, String url, Map<String, String> header, EGCCHandCallback hand) {
        post(param, url, hand, "application/json;charset=UTF-8", header);
    }

    /***
     * post  requestbody请求
     * @param param
     * @param url
     */
    public void postRequestBody(JSONArray param, String url, Map<String, String> header, EGCCHandCallback hand) {
        post(param, url, hand, "application/json;charset=UTF-8", header);
    }

    /***
     * post  表单请求
     * @param param
     * @param url
     */
    public void postFormData(JSONObject param, String url, EGCCHandCallback hand) {
        post(param, url, hand, "application/x-www-form-urlencoded;charset=UTF-8", null);
    }

    /***
     * post  表单请求
     * @param param
     * @param url
     */
    private void post(Object param, String url, EGCCHandCallback hand, String media, Map<String, String> header) {
        request(param, url, hand, media, HttpMethod.POST, header);
    }

    /***
     * get  请求
     * @param param
     * @param url
     */
    public void get(Map<Object, Object> param, String url, EGCCHandCallback hand) {
//        request(param, url, hand, "application/x-www-form-urlencoded;charset=UTF-8", HttpMethod.GET, null);
        String result = restTemplate.getForObject(url, String.class, param);
        hand.callBack(result);
    }

    /***
     * post  表单请求
     * @param param
     * @param url
     */
    private void request(Object param, String url, EGCCHandCallback hand, String media, HttpMethod httpMethod, Map<String, String> header) {
        //判断是否是https请求
        isHttps(url);
        HttpHeaders httpHeaders = new HttpHeaders();
        MediaType type = MediaType.parseMediaType(media);
        httpHeaders.setContentType(type);
        if (header != null) {
            header.forEach((k, v) -> {
                httpHeaders.add(k, v);
            });
        }
        HttpEntity<Object> entity = new HttpEntity<>(param, httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, httpMethod, entity, String.class);
        if (response.getStatusCode() == HttpStatus.OK) {
            //请求成功
            hand.callBack(response.getBody());
        } else {
            throw new CustomParameterizedException(String.format("%s:接口请求失败", url));
        }
    }

    /**
     * 判断是否是https
     *
     * @param url
     */
    public void isHttps(String url) {
        if (StrUtil.isNotEmpty(url) && url.toLowerCase(Locale.ROOT).startsWith("https")) {
            restTemplate.setRequestFactory(new HttpsClientRequestFactory());
        }
    }


    public SimpleClientHttpRequestFactory getFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //单位为ms
        factory.setReadTimeout(10 * 1000);
        //单位为ms
        factory.setConnectTimeout(30 * 1000);
        // 代理的url网址或ip, port端口http://cmproxy.gmcc.net
        //端口8081
        InetSocketAddress address = new InetSocketAddress("http://cmproxy.gmcc.net", 8081);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
        factory.setProxy(proxy);
        return factory;
    }


    public interface EGCCHandCallback {

        void callBack(String result);
    }
}
